<html>
<head><meta charset="utf-8"><title>error: no name for root_crate · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/error.3A.20no.20name.20for.20root_crate.html">error: no name for root_crate</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236870452"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/error%3A%20no%20name%20for%20root_crate/near/236870452" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tdelabro <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/error.3A.20no.20name.20for.20root_crate.html#236870452">(Apr 30 2021 at 17:19)</a>:</h4>
<p>Hey, I'm doing some refacto in the librustdoc crate, trying to do more on-demand computation of values. <br>
We have this struct, doctree::Module:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">crate</span><span class="w"> </span><span class="k">struct</span> <span class="nc">Module</span><span class="o">&lt;'</span><span class="na">hir</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">    </span><span class="k">crate</span><span class="w"> </span><span class="n">name</span>: <span class="nc">Symbol</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="k">crate</span><span class="w"> </span><span class="n">id</span>: <span class="nc">hir</span>::<span class="n">HirId</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Which later implement the Clean trait:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">Clean</span><span class="o">&lt;</span><span class="n">Item</span><span class="o">&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">doctree</span>::<span class="n">Module</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="w">  </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">clean</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">cx</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="n">DocContext</span><span class="o">&lt;'</span><span class="nb">_</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Item</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">          </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="n">Item</span>::<span class="n">from_hir_id_and_parts</span><span class="p">(</span><span class="w"></span>
<span class="w">            </span><span class="bp">self</span><span class="p">.</span><span class="n">id</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="nb">Some</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">name</span><span class="p">),</span><span class="w"></span>
<span class="w">            </span><span class="n">ModuleItem</span><span class="p">(</span><span class="n">Module</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">items</span><span class="p">,</span><span class="w"> </span><span class="n">span</span><span class="w"> </span><span class="p">}),</span><span class="w"></span>
<span class="w">            </span><span class="n">cx</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>What I want to do is replace <code>Some(self.name)</code> by a call to <code>cx.tcx.hir().name(self.id)</code>, but when I do so it panics with the following error:<br>
<code>error: internal compiler error: compiler/rustc_middle/src/hir/map/mod.rs:812:21: no name for root_crate</code></p>
<p>What's the logic behind it ?</p>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> said that at worst we can special case the root_crate, but  is there another way to make it work ?</p>



<a name="236872517"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/error%3A%20no%20name%20for%20root_crate/near/236872517" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> tdelabro <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/error.3A.20no.20name.20for.20root_crate.html#236872517">(Apr 30 2021 at 17:32)</a>:</h4>
<p>This fixes it:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">    </span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">opt_name</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">id</span>: <span class="nc">HirId</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;</span><span class="n">Symbol</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="nb">Some</span><span class="p">(</span><span class="k">match</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="n">id</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">Item</span><span class="p">(</span><span class="n">i</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">i</span><span class="p">.</span><span class="n">ident</span><span class="p">.</span><span class="n">name</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">ForeignItem</span><span class="p">(</span><span class="n">fi</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">fi</span><span class="p">.</span><span class="n">ident</span><span class="p">.</span><span class="n">name</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">ImplItem</span><span class="p">(</span><span class="n">ii</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">ii</span><span class="p">.</span><span class="n">ident</span><span class="p">.</span><span class="n">name</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">TraitItem</span><span class="p">(</span><span class="n">ti</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">ti</span><span class="p">.</span><span class="n">ident</span><span class="p">.</span><span class="n">name</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">Variant</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">v</span><span class="p">.</span><span class="n">ident</span><span class="p">.</span><span class="n">name</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">Field</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">f</span><span class="p">.</span><span class="n">ident</span><span class="p">.</span><span class="n">name</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">Lifetime</span><span class="p">(</span><span class="n">lt</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">lt</span><span class="p">.</span><span class="n">name</span><span class="p">.</span><span class="n">ident</span><span class="p">().</span><span class="n">name</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">GenericParam</span><span class="p">(</span><span class="n">param</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">param</span><span class="p">.</span><span class="n">name</span><span class="p">.</span><span class="n">ident</span><span class="p">().</span><span class="n">name</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">Binding</span><span class="p">(</span><span class="o">&amp;</span><span class="n">Pat</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">kind</span>: <span class="nc">PatKind</span>::<span class="n">Binding</span><span class="p">(</span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">l</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">),</span><span class="w"> </span><span class="o">..</span><span class="w"> </span><span class="p">})</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">l</span><span class="p">.</span><span class="n">name</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">Ctor</span><span class="p">(</span><span class="o">..</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="bp">self</span><span class="p">.</span><span class="n">get_parent_item</span><span class="p">(</span><span class="n">id</span><span class="p">)),</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">MacroDef</span><span class="p">(</span><span class="n">md</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">md</span><span class="p">.</span><span class="n">ident</span><span class="p">.</span><span class="n">name</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">Node</span>::<span class="n">Crate</span><span class="p">(</span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">crate_name</span><span class="p">,</span><span class="w">          </span><span class="c1">// &lt;--- here</span>
<span class="w">            </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">None</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">})</span><span class="w"></span>
</code></pre></div>
<p>Is it wrong to add this ?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>